<?php
# Part of RASS unittest fixtures: user mgmt.
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

define('USER2', 'unittest2');
define('EMAIL2', 'unittest2@rambla.be');
define('PWD2', 'unittest2');

class RassUserMgmtTest extends UnitTestCase {
    function TestRassClientlib() {
        $this->UnitTestCase("Rass User Mgmt Tests");
    }

    function testCreateUser() {
      try {
        $rass = new Rass(SUPERUSER, SUPERPWD, BASE);
        $newUser = $rass->createUser(USER, EMAIL, PWD);
        $this->assertEqual($newUser->content->params->username->text, USER);
        $this->assertEqual($newUser->content->params->password->text, USER);
        $this->assertEqual($newUser->content->params->email->text, EMAIL);
        $this->assertEqual($rass->getResponse()->getStatus(), 201);
        $this->assertEqual($rass->getResponse()->getHeader("Location"), BASE . "user/" . USER);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }

    function testCreateSameUser() {
      # try to create the same user again
      $status = 0;
      try {
        $rass = new Rass(SUPERUSER, SUPERPWD, BASE);
        $newUser = $rass->createUser(USER, EMAIL, PWD);
      }
      catch(Zend_Gdata_App_Exception $e) {
        $status = $e->getResponse()->getStatus();
      }
      $this->assertEqual($status, 409); # user already exists
    }

  
    function testUpdateUser() {    
      # login with new user
      try {
        $rass = new Rass(USER, PWD, BASE);
        $newUser = $rass->getUserEntry();
        $this->assertEqual($newUser->content->params->username->text, USER);
        $this->assertEqual($newUser->content->params->email->text, EMAIL);
        
        # update user email
        $updatedUser = $rass->updateUserEntry("new_unittest@rambla.be");
        $this->assertEqual($updatedUser->content->params->email->text, "new_unittest@rambla.be");
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }
    
    function createAnotherUser() {    
      try {
        # create another user
        $rass2 = new Rass(SUPERUSER, SUPERPWD, BASE);
        $newUser2 = $rass2->createUser(USER2, EMAIL2, PWD2);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    
      # try to user 1 to delete user 2 => should fail
      try {
        $rass = new Rass(USER, PWD, BASE);
        $rass->deleteUser(USER2);
        assert(false);
      }
      catch(Zend_Gdata_App_Exception $e) {
        $this->assertEqual($e->getResponse()->getStatus(), 403);
      }
      
      try {
        # delete the second user with the superuser
        $rass2->deleteUser(USER2);
        $this->assertEqual($rass2->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }
    
    function testDeleteUser() {
      try {
        $rass = new Rass(SUPERUSER, SUPERPWD, BASE);
        $newUser = $rass->deleteUser(USER);
        $this->assertEqual($rass->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }
    
}